/**
 * 
 */
package com.sduept.nwld.mobile.manager.fault;

import com.sduept.bigdata.fault.entity.FaultReportMain;
import com.sduept.bigdata.fault.entity.FaultReportStationDetail;
import com.sduept.core.dao.SuperDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 
 * Simple to Introduction
 * 
 * @Description: [变电站故障详情查询]
 * @Author: [lixz]
 * @CreateDate: [2019年6月18日 上午11:26:36]
 * 
 * 
 */
@Service
public class FaultReportStationDetailManager {
	@Autowired
	private SuperDAO dao;
	@PersistenceContext(unitName = "primaryUnit")
	private EntityManager em;

	/**
	 * 查询所有变电站故障详情
	 *
	 * @return
	 */
	public List<FaultReportStationDetail> getFaultReportStationDetailAll() {
		 List<FaultReportStationDetail>  list = dao.findAll(FaultReportStationDetail.class);
		return list;
	}
	/**
	 * 通过主键id查询变电站故障详情
	 * 
	 * @param id
	 * @return
	 */
	public FaultReportStationDetail getFaultReportStationDetailById(String id) {
		return dao.findById(id, FaultReportStationDetail.class);
	}

	/**
	 * 通过故障id和变电站id查询变电站故障详情
	 * 
	 * @param faultId
	 * @param stationId
	 * @return
	 */
	public FaultReportStationDetail getFaultReportStationDetailById(String faultId, String stationId) {
		List<FaultReportMain> faultReportMainList = dao.findByCondition(FaultReportMain.class, "frid=?1", faultId);
		if(faultReportMainList == null||!(faultReportMainList.size() > 0)) {
			return null;
		}
		FaultReportMain faultReportMain = faultReportMainList.get(0);
		Set<FaultReportStationDetail> faultReportStationDetailSet = faultReportMain.getFaultReportStationDetails();
		em.detach(faultReportMain);
		for (Iterator<FaultReportStationDetail> iter = faultReportStationDetailSet.iterator(); iter.hasNext();) {
			FaultReportStationDetail faultReportStationDetailIterator = iter.next();
			em.detach(faultReportStationDetailIterator);
		     if(faultReportStationDetailIterator.getStationId().equals(stationId)) {
		    	 return faultReportStationDetailIterator;
		     }
		 }
		return null;
	}
}